<?php
/**
 * @author zhaofei
 * @package 终端管理 
 * @createtime 2014-11-27
 * @updatetime 2014-12-08
 * @ps:暂为最高权限用户
 */
namespace System\Controller;
use Think\Controller;
class EmployeeController extends Controller {
	function __construct(){
		parent::__construct();
		$ranks = session('ranks');
		if($ranks!=1){
			$this->error('对不起你没有权限',U('Login/index'));
		}
	}

    public function index(){
        $model = M('Employee'); // 实例化Employee对象
        $count = $model->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $model->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
    }

    public function add(){
        $this->display();
    }

    public function addEmployee(){
        $data = D('Employee')->create();
        if($data==false){
            $this->error(D('Employee')->getError());
        }
        if(!I('post.id')){
            $rs = D('Employee')->add($data);
            if($rs){
                $this->success('添加信息成功,关联终端成功',U('index'));
            }else{
                $this->error('系统故障');
            }  
        }else{
            $rs = D('Employee')->where(array('id'=>I('post.id')))->save($data);
            if($rs){
                $this->success('修改成功',U('index'));
            }else{
                $this->error('系统故障');
            }  
        }
    }


    public function edit(){
        if(I('post.id')){
            $data = D('Employee')->create();
            if($data==false){
                $this->error(D('Employee')->getError());
            }else{
                $data['password'] = md5(md5($data['password'],TRUE));
                $rs = D('Employee')->where(array('id'=>I('post.id')))->save($data);
                if($rs){
                    $this->success('修改成功',U('index'));
                }else{
                    $this->error('系统故障');
                } 
            }
        }else{
            if(I('get.id')){
                 $Employee = D('Employee')->where(array('id'=>I('get.id')))->find();
                 $this->assign('Employee',$Employee);
                 $data = D('Employee')->where(array('ranks'=>array('neq',4)))
                     ->field('id,true_name')->select();
                 $this->assign('data',$data);
                 $this->display();
            }else{
                $this->error('参数错误');
            }
        }
        
    }

    public function del(){
        $id = I('get.id');
        if(!$id){
            $this->error('参数错误');
        }
        $rs = D('Employee')->delete($id);
            //清除相应终端
        D('EmStore')->where(array('e_id'=>$id))->delete();
        // echo D('EmStore')->getLastSql();
        // exit();
        //清除报单
        D('Order')->where(array('e_id'=>$id))->delete();
        if($rs){
            $this->success('删除成功',U('index'));
        }else{
            $this->error('系统故障');
        }
    }

    public function getStoreList(){
        $cons = array(
            'provice'=>I('post.province'),
            'city'=>I('post.city'),
            'area'=>I('post.area'),
            'status'=>1
            );
        $data = D('Store')->field('id,name')
        ->where($cons)->order('sort asc')->select();
        // echo D('Store')->getLastSql();
        if($data){
            $this->ajaxReturn(array('status'=>true,'data'=>$data));
        }else{
            $this->ajaxReturn(array('status'=>false,'data'=>'无'));

        }
    }


    //业务终端管理

    public function em_store(){
        $id = I('get.id');
        if(!$id){
            $this->error('参数错误');
        }
        $staff = D('Employee')->find($id);
        $store = D('EmStore')->where(array('e_id'=>$id))->find();
        if($store){
            $this->assign('store',$store);
        }
        $this->assign('staff',$staff);
        $this->display();
    }

     public function em_store_save(){
        $id = I('post.id');
        $data = D('EmStore')->create();
        if($data){
            if($id){
               $rs = D('EmStore')->where(array('id'=>$id))->save($data);
               $msg = '修改成功';
            }else{
               $rs = D('EmStore')->add($data);
               $msg = '添加成功';
            }
            if($rs){
                $this->success($msg);
            }else{
                $this->error('系统错误');
            }
        }else{
            $this->error(D('EmStore')->getError());
            exit();
        }
    }
 




    //终端导入
    public function importExcel(){
        $return=$this->localUpload(array('xls'));
        if ($return['error']){
            $this->error($return['msg']);
        }else {
            vendor('Spreadsheet');
            $data = new \Spreadsheet_Excel_Reader();
            // 设置输入编码 UTF-8/GB2312/CP936等等
            $data->setOutputEncoding('UTF-8');
            $filename = str_replace('http://'.$_SERVER['HTTP_HOST'],$_SERVER['DOCUMENT_ROOT'],$return['msg']);
            $data->read($filename);
            chmod(str_replace('http://'.$_SERVER['HTTP_HOST'],$_SERVER['DOCUMENT_ROOT'],$return['msg']),0777);
            //
            chmod($filename,0777);
            //
            $sheet=$data->sheets[0];
            $rows=$sheet['cells'];
         
            if ($rows){
                $i=0;
                $num=0;
                $time = time();
                foreach ($rows as $r){
                    if ($i!=0){
                        $Two = array();
                        $db=M('Employee');
                        $data = array(
                            'true_name'=>trim($r[1]),
                            'tel'=>trim($r[2]),
                            'qq'=>trim($r[3]),
                            'email'=>trim($r[4]),
                            'status'=>1,
                            'password'=>md5(md5(trim($r[5]),true)),
                            'ranks'=>$r[6]
                        );
                      //  echo D('EmployeeClass')->getLastSql();
                        $rs = D('Employee')->create($data);
                        if($rs){
                            $db->add($rs);
                            ++$num;
                        } 
                    }
                    $i++;
                }
            }
             $this->success('导入'.$num.'条',U('index'));
        }
    }


    function localUpload($filetypes=''){
        $config = array(
            'maxSize' => 1024*100,
            'rootPath' => './Uploads/',
            'savePath' => '',
            'saveName' => array('uniqid',''),
            'exts' => $filetypes,
            'autoSub' => true,
            'subName' => array('date','Ymd'),
        );

        $upload = new \Think\Upload($config);
        if (!file_exists($_SERVER['DOCUMENT_ROOT'].'/Uploads') || !is_dir($_SERVER['DOCUMENT_ROOT'].'/Uploads')){
            mkdir($_SERVER['DOCUMENT_ROOT'].'/Uploads',0777);
        }
        $info = $upload->uploadOne($_FILES['name']);

        if(!$info) {// 上传错误提示错误信息
            $error=1;
            $msg=$upload->getError();
        }else{// 上传成功 获取上传文件信息
            $error=0;
            $msg= 'http://'.$_SERVER['HTTP_HOST'].'/Uploads/'.$info['savepath'].$info['savename'];
        }
        return array('error'=>$error,'msg'=>$msg);
    }



     
    



}
